import com.opennews.openplatform.WorkflowProxy
import grails.util.Environment
import org.camunda.bpm.engine.spring.ProcessEngineFactoryBean
import org.camunda.bpm.engine.spring.SpringProcessEngineConfiguration
import org.springframework.jdbc.datasource.DataSourceTransactionManager
import org.springframework.jdbc.datasource.DriverManagerDataSource

// Place your Spring DSL code here
beans = {
    // region: Camunda workflow setup.
    // https://docs.camunda.org/get-started/spring/embedded-process-engine/
    // http://docs.grails.org/latest/guide/spring.html
    switch (Environment.current) {
        case Environment.PRODUCTION:
            dataSource(DriverManagerDataSource) {
                driverClassName = '${camunda.production.dataSource.driverClassName}'
                url = '${camunda.production.dataSource.url}'
                username = '${camunda.production.dataSource.username}'
                password = '${camunda.production.dataSource.password}'
            }
            break
        case Environment.DEVELOPMENT:
            dataSource(DriverManagerDataSource) {
                driverClassName = '${camunda.development.dataSource.driverClassName}'
                url = '${camunda.development.dataSource.url}'
                username = '${camunda.development.dataSource.username}'
                password = '${camunda.development.dataSource.password}'
            }
            break
    }

    transactionManager(DataSourceTransactionManager) {
        dataSource = ref('dataSource')
    }

    engineConfiguration(SpringProcessEngineConfiguration) {
        processEngineName = 'engine'
        dataSource = ref('dataSource')
        transactionManager = ref('transactionManager')
        databaseSchemaUpdate = true
        jobExecutorActivate = false
    }

    // region: Why configuration is different with official user guide for processEngine bean.
    //
    // These is the code from comunda official user guide.
    // But we found this actually is an instance of process engine instead of process engine factory.
    // It has no getObject method.
    //
    // That's why we setup this as process engine bean directly and it works.
    //
    // @Bean
    // public ProcessEngineFactoryBean engineFactory(SpringProcessEngineConfiguration engineConfiguration) {
    //     ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
    //     factoryBean.setProcessEngineConfiguration(engineConfiguration);
    //     return factoryBean;
    // }
    //
    // @Bean
    // public ProcessEngine processEngine(ProcessEngineFactoryBean factoryBean) throws Exception {
    //     return factoryBean.getObject();
    // }
    // endregion

    processEngine(ProcessEngineFactoryBean) {
        processEngineConfiguration = ref('engineConfiguration')
    }

    repositoryService(processEngine: 'getRepositoryService')
    runtimeService(processEngine: 'getRuntimeService')
    taskService(processEngine: 'getTaskService')
    formService(processEngine: 'getFormService')
    historyService(processEngine: 'getHistoryService')
    managementService(processEngine: 'getManagementService')

    workflowProxy(WorkflowProxy) {
        processEngine = ref('processEngine')
        repositoryService = ref('repositoryService')
        runtimeService = ref('runtimeService')
        taskService = ref('taskService')
        formService = ref('formService')
        historyService = ref('historyService')
        managementService = ref('managementService')
        processLocation = '${camunda.process.location}'
    }
    // endregion
}